<template>
  <div style="height: 100%;overflow: auto;margin-bottom: 20px;">
    <el-row class="nav">
      <i class="el-icon-arrow-left"
         @click="goBack"></i>
      <span class="font-weight">新建通知模板</span>
    </el-row>
    <el-divider style="margin-bottom:10px;"></el-divider>
    <el-form ref="template"
             :model="template"
             :label-position="left"
             :label-width="labelWidth"
             style="width: 500px;margin:0 auto;">
      <!-- 模板名称 -->
      <el-form-item label="">
        <p class="title-txt"></p>
        <el-row>
          <span>模板名称:&nbsp;&nbsp;</span>
          <el-input v-model="template.name"></el-input>
        </el-row>
      </el-form-item>
      <!-- 描述 -->
      <el-form-item>
        <el-row>
          <span>描述:&nbsp;&nbsp;</span>
          <el-input v-model="template.discription"></el-input>
        </el-row>
      </el-form-item>
      <!-- 模板类型 -->
      <!--<el-form-item>-->
        <!--<span>模板类型:&nbsp;&nbsp;</span>-->
        <!--<el-select v-model="Options"-->
                   <!--@change="reminderTime(item.dictCode)"-->
                   <!--size="mini"-->
                   <!--clearable-->
                   <!--placeholder="" value="">-->
          <!--<el-option v-for="item in typeOptions"-->
                     <!--:key="item.id"-->
                     <!--:label="item.dictName"-->
                     <!--:value="item.dictCode">-->
          <!--</el-option>-->
        <!--</el-select>-->
      <!--</el-form-item>-->

      <el-form-item>
        <el-row>
          <span style="float:left;">模板类型:&nbsp;&nbsp;</span>
          <el-select v-model="Options"
                     @change="reminderTime()"
                     size="mini"
                     clearable
                     placeholder="请选择模板类型" value="">
            <el-option v-for="item in typeOptions"
                       :key="item.tenantId"
                       :label="item.dictName"
                       :value="item.dictCode">
            </el-option>
          </el-select>
        </el-row>
      </el-form-item>
      <!-- 模板内容 -->
      <el-form-item>
        <span>模板内容:&nbsp;&nbsp;</span>
        <el-input v-model="template.content">
        </el-input>
      </el-form-item>

      <!-- 标题 -->
      <el-form-item>
        <el-row>
          <span>标题:&nbsp;&nbsp;</span>
          <el-input v-model="template.title"></el-input>
        </el-row>
      </el-form-item>

      <!-- 附件 -->
      <el-form-item>
        <el-row>
          <span>附件:&nbsp;&nbsp;</span>
          <el-input v-model="template.attachmentAddress"></el-input>
        </el-row>
      </el-form-item>
      <el-form-item>
        <el-button type="button"
                   class="el-button el-button--primary el-button--medium h30Btn"
                   @click="Submit">完成</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  import {
    getNoticeTemp,
    newAddMsgTemp,
    deleteListMsg,
    startClose,
    getListNotice,
    getInsertNotice,
    getInsertNoticeById
  } from '@/api/setNotice'
  import common from '@/common/common.js'
  export default {
    data () {
      return {
        form:{
          detailsCheckList:''
        },
        checked: '',
        detailViewList: [],
        nonSysDictParamList: [],
        typeOptions: [],
        Options:'',
        item: '短信',
        template: {
          name: '',
          discription: '',
          content: '',
          title: '',
        },
        newSetNoticeDate:[],
      }
    },
    mounted () {
      // 用唯一标识搜索模板
      this.getNoticeTemp();
      if(this.$store.state.newNoticeId){
        this.searchTmp()
      }
    },
    methods: {
      search () {
      },
      create () {
      },
      reminderTime () {
        //  短信
        if (this.Options === 'msg') {
            this
              .$router
              .push('/newSetNoticeTemplate')
        }
        //  邮件
        else if (this.Options === 'email') {
            this
              .$router
              .push('/newSetNoticeTemplateEmail')
        }
        //  系统消息
        else if (this.Options === 'sys_msg') {
            this
              .$router
              .push('/newSetNoticeTemplateMessage')
        }
      },
      searchTmp () {
        let params = this.$store.state.newNoticeId
        getInsertNoticeById(params).then(res => {
          if (res.data.retCode === 200) {
            this.template.name = res.data.data[0].name
            this.template.discription = res.data.data[0].discription
            this.template.content = res.data.data[0].content
            this.$message({ message: res.data.msg, type: 'success' })
          } else {
            this.$message({ message: res.data.msg, type: 'error' })
          }
        })
      },
      // 获取模板类型
      getNoticeTemp () {
        getNoticeTemp().then(res => {
          if (res.data.retCode === 200) {
            this.typeOptions = res.data.data
            console.log(res.data)
            this.$message({ message: res.data.msg, type: 'success' })
          } else {
            this.$message({ message: res.data.msg, type: 'error' })
          }
        })

      },
      Submit () {
//         新建
        let params = {
          name: this.template.name,
          templateType: this.Options,
          discription: this.template.discription,
          content: this.template.content,
          attachmentAddress: "附件地址",
          linkToWorkOrder: "Y"
        }
        console.log(this.typeOptions[0])
//        更新
        let updata = {
          name: this.template.name,
          id: this.$store.state.newNoticeId,
          templateType: this.Options,
          discription: this.template.discription,
          content: this.template.content,
          linkToWorkOrder: "Y"

        }
        if(this.$store.state.newWorkName){
          console.log(this.$store.state.newWorkName + '更新')
          // 更新模板
          getInsertNotice(updata).then(res => {
            if (res.data.retCode === 200) {

              this.$message({ message: res.data.msg, type: 'success' })
            } else {
              this.$message({ message: res.data.msg, type: 'error' })
            }
          })
        } else {
          // 新建模板
          newAddMsgTemp(params).then(res => {
            if (res.data.retCode === 200) {


              this.$message({ message: res.data.msg, type: 'success' })
            } else {
              this.$message({ message: res.data.msg, type: 'error' })
            }
          })
        }
      },
      goBack () {
        this.$router.push('/setNotice')
      }

    }

  }
</script>
<style scoped>
.title-txt {
  font-size: 12px;
  font-weight: bold;
  line-height: 25px;
}
</style>
